package cc.openkit.admin.controller.userInfo;

import cc.openkit.admin.model.GGroupLimit;
import cc.openkit.admin.model.User;
import cc.openkit.admin.model.UserInfo;
import cc.openkit.admin.service.g.GGroupLimitService;
import cc.openkit.admin.service.user.UserService;
import cc.openkit.admin.service.userInfo.UserInfoService;
import cc.openkit.admin.util.AppUtil;
import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.common.KitUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户详情管理
 *
 * @author bigben
 * @date 2018.2.5
 */
@Controller
@Scope("prototype")
@RequestMapping("/userInfo")
public class UserInfoController {
    private Logger log = Logger.getLogger(UserInfoController.class);

    @Resource
    private UserService userService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private GGroupLimitService gGroupLimitService;

    /**
     * 添加的方法
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public ModelAndView getAll(HttpServletRequest request){

        // 权限验证。这里的最后的 5 就是上面我们找到菜单的ID，需要在这里查找
        GGroupLimit gGroupLimit = gGroupLimitService.testGroup(request, 27);

        ModelAndView mv = new ModelAndView();

        // 没有权限，返回错误页面
        if(gGroupLimit==null){
            mv.setViewName("/admin/err/no_group_err");
            mv.addObject("msg", StaticFinalVar.NO_GROUP_MSG+ StaticFinalVar.CALL_GROUP);
            return mv;
        }

        // 取值
        mv.setViewName("/admin/userInfo/list"); // 需要跳转的页面
        mv.addObject("kitG",gGroupLimit);
        return mv;
    }

    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "/getAllJson", method = RequestMethod.POST)
    @ResponseBody
    public Object getAllJson(HttpServletRequest request)throws Exception{
        log.info("获取用户列表数据");

        Map<String,Object> map = new HashMap<String,Object>();
        // 分页需要的参数
        String page = request.getParameter("page");// 获得页数
        String limit = request.getParameter("limit");// 获得每页显示条数
        String search = request.getParameter("search");// 获取搜索条件

        // 封装数据
        UserInfo userInfo = new UserInfo();
        if(KitUtil.feikong(search)){
            userInfo.setUserName(search);
        }

        // 分页查询
        List<UserInfo> userInfoList = userInfoService.findByCondition(userInfo,Integer.valueOf(page), Integer.valueOf(limit));
        // 获取查询到的总数
        int size = userInfoService.queryCountByCondition(userInfo);

        // 返回数据
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",userInfoList);

        return JSONObject.toJSON(map);
    }



    /**
     * 通过认证
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/pass", method = RequestMethod.POST)
    @ResponseBody
    public Object del(HttpServletRequest request) throws Exception{
        log.info("通过认证");
        String id = request.getParameter("id");
        UserInfo userInfo = userInfoService.queryByUUID(id);
        userInfo.setUiStatus(3);//通过认证
        userInfo.setUiAuthenticationTime(new Date());
        int i = userInfoService.updateSelective(userInfo);
        return i==1?KitUtil.returnMap("200",StaticFinalVar.DEL_OK):KitUtil.returnMap("101",StaticFinalVar.DEL_ERR);
    }



    /**
     * 不通过页面
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/goNoPass", method = RequestMethod.GET)
    public ModelAndView goUpdate(HttpServletRequest request) throws Exception{
        log.info("用户管理 》 修改 》 跳转");

        String id = request.getParameter("id");

        ModelAndView mv = new ModelAndView();
        mv.setViewName("/admin/userInfo/noPass");
        mv.addObject("kitModel",id);
        return mv;
    }

    /**
     * 拒绝理由
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/noPass", method = RequestMethod.POST)
    @ResponseBody
    public Object update(HttpServletRequest request) throws Exception{
        log.info("拒绝理由");
        String uiId = request.getParameter("uiId");
        String uiContent = request.getParameter("uiContent");
        UserInfo userInfo = new UserInfo();
        userInfo.setUiId(uiId);
        userInfo.setUiStatus(4);
        userInfo.setUiContent(uiContent);
        userInfo.setUiAuthenticationTime(new Date());
        // 修改保存并返回参数
        return JSONObject.toJSON(userInfoService.updateSelective(userInfo)==1 ? KitUtil.returnMap("200",StaticFinalVar.UPDATE_OK) : KitUtil.returnMap("101",StaticFinalVar.UPDATE_ERR));
    }

}
